<template>
	<view class="title-bar">
		<view class=".title-background" :style="'height:'+phoneStateBarHeight+'px;'"></view>
		<view class="title .title-background">
			<view class="left">
				<uni-icons v-show="showBackButton" type="left" class="back-button" @click="handleBack"></uni-icons>
				<image class="team-icon" :src="teamIcon" @click="handleTeamClicked"></image>
			</view>
			<view class="body" @click="handleTitleClicked">
				<text class="main-title">{{ titleName }}</text>
				<view class="sub-title">
					<text>{{ projectName }}</text>
				</view>
			</view>
			<view class="right"></view>
		</view>
		<slot></slot>
	</view>
</template>

<script>
	import {getTeam} from '@/api/system/team.js';
	import config from '@/config'
	
	export default {
		name: 'PageHeader',
		data() {
			return {
				// 项目名称
				projectName: null,
				// 团队图标
				teamIcon: null,
			}
		},
		props: {
			/** 设置自定义标题 */
			title: {
				type: String,
				default: null
			},
			/** 是否显示返回按钮 */
			showBackButton: {
				type: Boolean,
				default: true
			},
			/** 显示的团队ID */
			teamId: {
				type: [Number, String],
				default: null
			},
			/** 是否禁止点击团队图标和项目名称操作 */
			disabled: {
				type: Boolean,
				default: false
			},
		},
		computed: {
			/** 当前页面标题 */
			titleName() {
				// 获取当前页面路径
				const pages = getCurrentPages()
				const currentPage = pages[pages.length - 1]
				const route = currentPage.route;
				// 获取页面json配置
				const pagesJson = require('@/pages.json')
				// 遍历查找当前页面的配置
				let title = null;
				for(const page of pagesJson.default.pages) {
					if (page.path === route) {
						title = page.style.navigationBarTitleText || '';
						break;
					}
				}
				// 如果title没有找到，从分包里查找
				if(!title) {
					for(const sub of pagesJson.default.subPackages) {
						for(const page of sub.pages) {
							if (`subpackage/${page.path}` === route) {
								title = page.style.navigationBarTitleText || ''
								break;
							}
						}
					}
				}
				title = this.title || title;
				if(this.$i18n && this.$i18n.te(title)) {
					return this.$i18n.t(title);
				} else {
					return title;
				}
			},
			/** 当前团队图标 */
			currentTeamIcon() {
				return this.$store.state.user.config.currentTeamIcon;
			},
			/** 当前项目名称 */
			currentProjectName() {
			    return this.$store.state.user.config.currentProjectName;
			},
			/** 当前团队ID */
			currentTeamId() {
			    return this.$store.state.user.config.currentTeamId;
			},
			/** 当前成员id */
			currentMemberId() {
				return this.$store.state.user.id;
			},
			phoneStateBarHeight() {
				return uni.getWindowInfo().statusBarHeight;
			}
		},
		onReady() {
			// 如果显示的不是当前团队，则从接口获取团队数据
			if(this.teamId && this.teamId !== this.currentTeamId) {
				getTeam(this.teamId).then(res=>{
					this.projectName = '';
					this.teamIcon = config.baseUrl + res.data.teamIcon;
				});
			} else { // 否则从缓存获取团队数据
				this.projectName = this.currentProjectName;
				this.teamIcon = this.currentTeamIcon;
			}
		},
		methods: {
			/** 返回上一页面 */
			handleBack() {
				uni.navigateBack();
			},
			/** 处理点击标题的事件 */
			handleTitleClicked() {
				if(this.disabled) return;
				this.$tab.navigateTo(`/subpackage/pages/system/project/project-switch/index`);
			},
			/** 处理点击团队图标的事件 */
			handleTeamClicked() {
				if(this.disabled) return;
				this.$tab.navigateTo(`/subpackage/pages/system/team/team-switch/index`);
			}
		}
	}
</script>

<style lang="scss" scoped>
	.title-bar {
		width: 100%;
		position: sticky;
		top: 0rpx;
		z-index: 1;
		background-color: $uni-bg-color-grey;
	}
	.title-background {
		background-color: $uni-bg-color;
	}
	.title {
		width: 100%;
		display: inline-flex;
		flex-direction: row;
		align-items: center;
		padding: 10rpx 20rpx;
		height: 76rpx;
		gap: 15rpx;
	}
	.title>.left {
		flex-shrink: 0;
		min-width: 32rpx;
		display: inline-flex;
		flex-direction: row;
		align-items: center;
		gap: 5rpx;
	}
	.team-icon {
		width: 60rpx;
		height: 60rpx;
		border: 1rpx solid #303133;
		border-radius: 5rpx;
	}
	.title>.right {
		flex-shrink: 0;
		min-width: 32rpx;
	}
	.title>.body {
		flex: 1;
		display: inline-flex;
		flex-direction: column;
		justify-content: center;
		align-items: flex-start;
	}
	.main-title {
		width: 100%;
		color: #303133;
		font-size: 25rpx;
	}
	.sub-title {
		width: 100%;
		color: #8f8f94;
		font-size: 22rpx;
		display: inline-flex;
		flex-direction: row;
		align-items: center;
		gap: 5rpx;
	}
	.back-button {
		width: 32rpx;
		height: 32rpx;
	    margin: 0rpx 10rpx 0 10rpx;
		padding:0rpx;
		border-width: 0rpx;
		background-color: #0000;
	    display: flex;
	    flex-direction: row;
	    align-items: center;
	    justify-content: center;
	}
	.back-button:after {
		width: 0;
		height: 0;
	}
	.back-button>.uni-text {
		line-height: 10rpx;
	}
</style>